/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.balisunrise.swing.transaction.panel;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.swing.builder.LayoutBuilder;

/**
 *
 * @author Glauco
 */
public class GridPanel extends JPanel{

    private JTable table;
    private JScrollPane scrollPane;

    private LayoutBuilder layoutBuilder;

    public GridPanel(JTable table, LayoutBuilder layoutBuilder) {
        super();
        this.table = table;
        this.layoutBuilder = layoutBuilder;
        createScrollPane();
    }

    public LayoutBuilder getLayoutBuilder() {
        return layoutBuilder;
    }

    public void setLayoutBuilder(LayoutBuilder layoutBuilder) {
        this.layoutBuilder = layoutBuilder;
    }

    public JTable getTable() {
        return table;
    }

    public void setTable(JTable table) {
        this.table = table;
        createScrollPane();
    }

    public JScrollPane getScrollPane() {
        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
        createLayout();
    }

    private void createScrollPane() {

        if (table == null)
            return ;

        // cria Scroll
        JScrollPane scroll = new JScrollPane(table);
        String name = table.getName().replace("_baligrid_", "_scrollPane_");
        scroll.setName(name);
        scroll.getAccessibleContext().setAccessibleName(name);

        // adciona o table ao scrollPane
        scroll.setViewportView(table);
        //scroll.getViewport().setSize(table.getPreferredSize());
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        this.scrollPane = scroll;

        createLayout();
    }

    private void createLayout(){

        List list = new ArrayList();
        list.add(scrollPane);

        try{
            layoutBuilder.create(this, list);
        }catch(BuilderException be){
            System.out.println(
                    "Erro em GridPanel ao criar layout.\n"+
                    be.getMessage());
        }
    }

}
